﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Kay.Client.Demo
{
    public sealed partial class Chat : Form
    {
        public ContactsInfo ContactsInfo { get; set; }

        private TcpInterNetworkStreamClient _client;

        private Dictionary<string, TransferFile> _transferFiles = new Dictionary<string, TransferFile>();

        public Chat(ContactsInfo info, TcpInterNetworkStreamClient client)
        {
            InitializeComponent();

            ContactsInfo = info;
            _client = client;

            Text = @"与 " + info.Name + @" 对话中…";
        }

        public void ReceivedText(string msg)
        {
            if (textBox_Message.InvokeRequired)
            {
                textBox_Message.Invoke(new Action<string>(ReceivedText), msg);
                return;
            }
            if (textBox_Message.Lines.Length > 0)
            {
                textBox_Message.AppendText(Environment.NewLine);
                textBox_Message.AppendText(Environment.NewLine);
            }
            textBox_Message.AppendText(ContactsInfo.Name + " 说：");
            textBox_Message.AppendText(Environment.NewLine);
            textBox_Message.AppendText(msg);
        }

        public void ReceivedRequestSendFile(ContactsInfo info, TransferFileInfo file)
        {
            if (flowLayoutPanel_TransferFiles.InvokeRequired)
            {
                flowLayoutPanel_TransferFiles.Invoke(
                    new Action<ContactsInfo, TransferFileInfo>(ReceivedRequestSendFile), info, file);
                return;
            }
            var trans = new TransferFile(info, file);
            _transferFiles.Add(file.SendId, trans);
            flowLayoutPanel_TransferFiles.Controls.Add(trans);
        }

        public void FileProgressChanged(string id, int pro)
        {
            if (_transferFiles[id].InvokeRequired)
            {
                _transferFiles[id].Invoke(new Action<string, int>(FileProgressChanged), id, pro);
                return;
            }
            _transferFiles[id].ChangePro(pro);
        }

        public void ContactsAgreeReciveFile(ContactsInfo info, TransferFileInfo file)
        {
            if (flowLayoutPanel_TransferFiles.InvokeRequired)
            {
                flowLayoutPanel_TransferFiles.Invoke(
                    new Action<ContactsInfo,TransferFileInfo>(ContactsAgreeReciveFile),info, file);
                return;
            }
            if (!file.Agree)
            {
                MessageBox.Show(this,
                    string.Format("{0} 拒绝接收文件！{1}文件名：{2}", ContactsInfo.Name, Environment.NewLine,
                        Path.GetFileName(file.SourceFileName)), @"提示", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }
            var trans = new TransferFile(ContactsInfo, file);
            _transferFiles.Add(file.SendId, trans);
            flowLayoutPanel_TransferFiles.Controls.Add(trans);
        }

        public void FileComplate(string id)
        {
            if (flowLayoutPanel_TransferFiles.InvokeRequired)
            {
                flowLayoutPanel_TransferFiles.Invoke(new Action<string>(FileComplate), id);
                return;
            }
            TransferFile u = null;
            foreach (Control control in flowLayoutPanel_TransferFiles.Controls)
            {
                var info = control as TransferFile;
                if (info == null) continue;
                if (info.File.SendId != id) continue;
                u = info;
                break;
            }
            if (u != null)
            {
                flowLayoutPanel_TransferFiles.Controls.Remove(u);
                u.Dispose();
            }
            if (_transferFiles.ContainsKey(id))
            {
                _transferFiles.Remove(id);
            }
            GC.Collect();
        }

        private void button_Send_Click(object sender, EventArgs e)
        {
            if (textBox_Send.Text == string.Empty) return;
            _client.SendMessage(ContactsInfo.Id, textBox_Send.Text);
            if (textBox_Message.Lines.Length > 0)
            {
                textBox_Message.AppendText(Environment.NewLine);
                textBox_Message.AppendText(Environment.NewLine);
            }
            textBox_Message.AppendText("我 说" + "：");
            textBox_Message.AppendText(Environment.NewLine);
            textBox_Message.AppendText(textBox_Send.Text);
            textBox_Send.Clear();
        }

        private void button_SendFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                _client.SendFile(ContactsInfo.Id, openFileDialog.FileName);
            }
        }
    }
}
